import jieba
from collections import Counter

with open("西游记.txt", "r", encoding="utf-8") as f:
    text = f.read()

excludes = {"一个", "那里", "怎么", "我们", "不知", "两个", "甚么",
            "只见", "不是", "原来", "不敢", "闻言", "如何", "甚么", "么"}

words = jieba.cut(text)

counts = Counter()
for word in words:
    if len(word) == 1:
        continue  # 跳过单个字符
    elif word in ["行者", "大圣", "老孙"]:
        word = "悟空"
    elif word in ["师父", "三藏", "长老"]:
        word = "唐僧"
    elif word in ["悟净", "沙和尚"]:
        word = "沙僧"
    else:
        counts[word] += 1

for word in excludes:
    if word in counts:
        del counts[word]

sorted_counts = sorted(counts.items(), key=lambda x: x[1], reverse=True)

for i, (word, count) in enumerate(sorted_counts[:8]):
    print(f"{i+1}. {word}: {count}")







